<?php

namespace app\controller;

use app\BaseController;
use app\model\AppFun;
use app\utils\CamlHump;
use app\utils\ResposeUtils;
use think\facade\Db;
use think\facade\Request;

class AppFunController extends  BaseController
{


    function list()
    {
        $body = Request::post();
        $query = AppFun::createQuery($body);
        $list=CamlHump::toCamlArray($query->select());


        /*填充havenext*/
        for($i=0;$i<count($list);$i++){
            $haveNext=false;
            $parentObj=null;
            if(isset($list[$i]['id'])&&
                $list[$i]['id']>0
            ){
                $parentObj = AppFun::findOneByParentById($list[$i]['id']);
                $haveNext = $parentObj['id'] ?? false;
            }
            $list[$i]['haveNext'] = $haveNext;
            $list[$i]['parentObj'] = $parentObj;
        }


        return ResposeUtils::ok(
            $list,
            200,
            $query->count()
        );
    }



   function getbyid()
    {
        
        $id = Request::get("id");
        $one = AppFun::findOne($id);
        return ResposeUtils::ok(
            $one
        );
    }
    function update()
    {
        $body = Request::post();
        $updateersuilt=AppFun::updatePlus($body,$body['id']);
        return ResposeUtils::ok(
            $updateersuilt['id']
        );
    }
    function add()
    {
        $body = Request::post();
        $result=AppFun::addPlus($body);
        return ResposeUtils::ok(
            $result['id']
        );
    }
    function delbyid()
    {
        $id = Request::get('id');
        $result=AppFun::destroy($id);
        return ResposeUtils::ok(
            $result
        );
    }
}
